﻿using QBWeb.Models;
using QBWeb.ViewModels;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using QBWeb.Controllers;
using QBWeb.Models.QuestionFolder;
using QBWeb.Models.TestFolder;

namespace QBWeb.BLL
{
    /// <summary>
    /// 题库模块接口类
    /// </summary>
    public class QuestionBll
    {
        private static readonly QBDbContext db = new QBDbContext();
        private static readonly SetSqlTools setSql = new SetSqlTools();

        /// <summary>
        /// 将题目保存到试卷中
        /// </summary>
        /// <param name="testPaperId"></param>
        /// <param name="saveQuestion"></param>
        private static void SetTestDetails(int testPaperId,List<SaveQuestion> saveQuestion)
        {
            List<TestDetailsTb> testDetailsTb = db.TestDetails.Where(t => t.TestPaperTbId == testPaperId).ToList();
            foreach (var sq in saveQuestion)
            {
                if(testDetailsTb.Where(t => t.QuestId == sq.QuestId).Count() == 0)
                {
                    var newTest = new TestDetailsTb
                    {
                        TestPaperTbId = testPaperId,
                        QuestId = sq.QuestId,
                        Score = sq.Score,
                        QuestIndex = sq.Index
                    };
                    db.TestDetails.Add(newTest);
                }
                else
                {
                    var test = testDetailsTb.First(t => t.QuestId == sq.QuestId);
                    test.Score = sq.Score;
                    test.QuestIndex = sq.Index;
                    db.Entry(test).State = EntityState.Modified;
                }
            }
            db.SaveChanges();
        }

        public static void SaveQuestions(AllQuestionModels viewModle,string teacherId,int cid)
        {
            int testId=0;
            if (viewModle.Singles.Count != 0)
            {
                testId = viewModle.Singles.First().TestDetailsId;
            }
            else if (viewModle.Blanks.Count != 0)
            {
                testId = viewModle.Blanks.First().TestDetailsId;
            }
            else if (viewModle.TureFlases.Count != 0)
            {
                testId = viewModle.TureFlases.First().TestDetailsId;
            }
            var ts = new List<SaveQuestion>();
            //取课程关键词
            var smallKeywords = db.SmallKnowModuleTbs.Where(a => a.CurriculumId == cid).ToList();
            WordImportController word = new WordImportController();
            int? smallId;
            if (viewModle.Singles.Count != 0)
            {
                foreach (var item in viewModle.Singles)
                {
                    smallId=word.Classification(smallKeywords, item.KeyWords);//分类id
                    QuestionTb questionTb;
                    if (Convert.ToInt32(item.DataId) <= 0)//判断该题是否存在
                    {
                        questionTb = setSql.SetQuestionTb(item.QuestName, smallId, item.QuestType, item.QuestAnalysis, item.Level,teacherId,item.KeyWords);
                        db.QuestionTbs.Add(questionTb);
                        db.SaveChanges();
                        var singleChoiceTb = setSql.SetSingleChoiceTb(questionTb.Id, item.Options.ToArray(), item.ChoiceValue.ToArray(), item.ChoiceChecked.ToArray(), item.QuestType);
                        for (int x = 0; x < singleChoiceTb.Count; x++)
                        {
                            db.SingleChoiceTbs.Add(singleChoiceTb[x]);
                        }
                        db.SaveChanges();
                    }
                    else
                    {
                        questionTb = setSql.SetQuestionTb(db.QuestionTbs.Find(item.DataId), item.QuestName, item.QuestType, item.QuestAnalysis, item.Level);
                        db.Entry(questionTb).State = EntityState.Modified;
                        db.SaveChanges();
                        //缺少选择题更新
                    }
                    SaveQuestion sq = new SaveQuestion()
                    {
                        Index = item.QuestionIndex,
                        QuestId = questionTb.Id,
                        Score = item.Score
                    };
                    ts.Add(sq);
                }
            }
            if(viewModle.Blanks.Count != 0)
            {
                foreach (var item in viewModle.Blanks)
                {
                    smallId = word.Classification(smallKeywords, item.KeyWords);
                    QuestionTb questionTb;
                    if (Convert.ToInt32(item.DataId) <= 0)//判断该题是否存在
                    {
                        questionTb = setSql.SetQuestionTb(item.QuestName, smallId,item.QuestType, item.QuestAnalysis, item.Level,teacherId,item.KeyWords);
                        db.QuestionTbs.Add(questionTb);
                        db.SaveChanges();
                        var blanksTb = setSql.SetBlanksTb(questionTb.Id, item.CorrectValue, item.QuestType);
                        db.BlanksTbs.Add(blanksTb);
                        db.SaveChanges();
                    }
                    else
                    {
                        questionTb = setSql.SetQuestionTb(db.QuestionTbs.Find(item.DataId), item.QuestName,item.QuestType, item.QuestAnalysis, item.Level);
                        db.Entry(questionTb).State = EntityState.Modified;
                        db.SaveChanges();
                        //缺少填空题更新
                    }
                    SaveQuestion sq = new SaveQuestion()
                    {
                        Index = item.QuestionIndex,
                        QuestId = questionTb.Id,
                        Score = item.Score
                    };
                    ts.Add(sq);
                }
            }
            if(viewModle.TureFlases.Count != 0)
            {
                foreach (var item in viewModle.TureFlases)
                {
                    smallId = word.Classification(smallKeywords, item.KeyWords);
                    QuestionTb questionTb;
                    if (Convert.ToInt32(item.DataId) <= 0)//判断该题是否存在
                    {
                        questionTb = setSql.SetQuestionTb(item.QuestName, smallId, item.QuestType, item.QuestAnalysis, item.Level,teacherId,item.KeyWords);
                        db.QuestionTbs.Add(questionTb);
                        db.SaveChanges();
                        var trueFalseTb = setSql.SetTrueFalseTb(questionTb.Id, Convert.ToInt32(item.CorrectValue), item.QuestType);
                        db.TrueFalseTbs.Add(trueFalseTb);
                        db.SaveChanges();
                    }
                    else
                    {
                        questionTb = setSql.SetQuestionTb(db.QuestionTbs.Find(item.DataId), item.QuestName, item.QuestType, item.QuestAnalysis, item.Level);
                        db.Entry(questionTb).State = EntityState.Modified;

                        var tureFalseTb = setSql.UpdateTrueFalseTb(db.TrueFalseTbs.First(t => t.QuestionTbId == item.DataId), Convert.ToInt32(item.CorrectValue));
                        db.Entry(tureFalseTb).State = EntityState.Modified;
                        db.SaveChanges();
                    }
                    SaveQuestion sq = new SaveQuestion()
                    {
                        Index = item.QuestionIndex,
                        QuestId = questionTb.Id,
                        Score = item.Score
                    };
                    ts.Add(sq);
                }
            }
            SetTestDetails(testId,ts);
        }
    }
}